package com.yunupmall.controller.ford;

import cn.hutool.core.util.ObjectUtil;
import com.yunupmall.api.po.ShopingUpDataPo;
import com.yunupmall.api.po.SteamShopingDataPO;
import com.yunupmall.entity.admin.memberpojo.Member;
import com.yunupmall.entity.ford.FordShopingMupData;
import com.yunupmall.service.admin.service.SpecsCommdityService;
import com.yunupmall.service.ford.service.ShopingService;
import com.yunupmall.uits.Result;
import com.yunupmall.uits.SessionUtis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ShopingController {
    @Autowired
    private  ShopingService fordShopingService;
    @Autowired
    private Result result;
    @Autowired
    private SpecsCommdityService specCommdityService;

    private List<ShopingUpDataPo> dataPoList;

    @PostMapping("/setShopping")
    @ResponseBody
    public Result<String> setShopping(Integer s_id, Integer counts) {
        Member member;
        try {
            member =(Member) SessionUtis.getSessionAttribute("member");
        } catch (NumberFormatException e) {
           return result.error("没有获取到用户信息");
        }
        if (ObjectUtil.isEmpty(member)) {
            return result.error("没有获取到用户信息");
        } else {
            int i = fordShopingService.insertShoppingData(s_id, counts, member.getId());
            if (i > 0) {
                return result.ok("新增成功");
            }else {
                return result.error("新增失败");
            }
        }
    }

    @GetMapping("/getmembershopData")
    @ResponseBody
    public  Result<List<FordShopingMupData>> getmembershopData() {
        try {
            Member member =(Member) SessionUtis.getSessionAttribute("member");
            if (ObjectUtil.isEmpty(member)) {
                throw new RuntimeException("请先登录");
            }
            List<FordShopingMupData> fordShopingMupData = fordShopingService.queryshopData(member.getId());
            return result.ok(fordShopingMupData);

        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    @GetMapping("/deleteshopingID")
    @ResponseBody
    public Result<String> deleteshopingID(@RequestParam("id") Integer id) {
        try {
            return result.ok(fordShopingService.deleteshopingID(id));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    /**
     * 整合数据
     * @return
     */
    @PostMapping("/statement")
    @ResponseBody
    public Result getMes(@RequestBody List<ShopingUpDataPo> shopingUpDataPoList ){
        try {
            dataPoList =new ArrayList<ShopingUpDataPo>();
            dataPoList.addAll(shopingUpDataPoList);
            return result.ok("成功");
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
    /**
     * 获取递交的购物车信息
     */
    @GetMapping("/getShopingData")
    @ResponseBody
   public Result<List<SteamShopingDataPO>> getShopingData(){
       try {
           return result.ok(fordShopingService.combinationShopingData(dataPoList));
       }catch (Exception e) {
           return result.error(e.getMessage());
    }
    }
}
